package Dao.StudentDao;

import Entity.SelectCourse;
import Entity.Student;
import Util.SQLParse;
import Util.SQLUtil;
import com.sun.org.apache.bcel.internal.generic.Select;
import org.omg.CosNaming.NamingContextExtPackage.StringNameHelper;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/*
学生后台 Dao层 负责SQL语句 与传入参数的传入
 */
public class StudentSQLDao {

    private  SQLUtil sqlUtil = new SQLUtil();

    /*
    插入一个学生
    需要参数 学生所有信息(出了描述信息)
    返回数据库修改成功个数
     */
    public int insertStudent(Student student){

        Object studentId = student.getStudentId();
        Object studentPassword = student.getStudentPassword();
        Object studentName = student.getStudentName();
        Object studentMajor  = student.getStudentMajor();

        Object object[] = new Object[]{studentId,studentPassword,studentName,studentMajor};

        String sql = "insert into studentInfo(studentId,studentPassword,studentName,studentMajor) values(?,?,?,?)";

        return sqlUtil.execute(sql,object);
    }


    /*
    查看id是否存在
    需要参数 学生Id
    返回数据库查询到的个数
     */
    public int queryIdIsExist(String studentId){
        Object object[] = new Object[]{studentId};
        String sql = "select count(studentId) from studentinfo where studentId = ? ";
        ResultSet rs = sqlUtil.executeQuery(sql,object);
        //解析返回结果集行数的数量
        return SQLParse.parseCount(rs);
    }


    /*
    学生登录 需要参数 学生id 学生密码
    返回查询到的学生 0或1个
     */
    public Student selectStudentByStudentIdAndPassword(Student student){

        Object object[] = new Object[]{student.getStudentId(),student.getStudentPassword()};

        String sql = "select *from studentinfo where studentId = ? and studentPassword = ?";

        Student stu = selectStudent(student,sql,object);
        if (stu!=null) return stu;
        return null;
    }


    /*
    登录之后在后台内 不需要密码 可直修改个人信息 通过id直接修改
    需要参数 学生Id
     返回整个学生信息
     */
    public Student selectStudentNoPassword(Student student){

        Object object[] = new Object[]{student.getStudentId()};

        String sql = "select *from studentinfo where studentId = ?";

        Student stu = selectStudent(student,sql,object);
        if (stu!=null) return stu;
        return null;
    }



    /*
    登陆状态 通过学生id修改密码
    需要参数 学生账号 学生密码
    返回数据库修改成功个数
     */
    public int modifyPassword(Student student){

        Object object[] = new Object[]{student.getStudentPassword(),student.getStudentId()};
        String sql = "UPDATE studentinfo set studentPassword = ? where studentId = ?";
        return sqlUtil.execute(sql,object);
    }


    /*
    修改学生信息 成绩与描述
    需要信息学生描述  学生Id  学生成绩
    返回数据库修改成功个数
     */
    public int updateByStudentId(Student student){
        String studentId = student.getStudentId();
        float totalCredits = student.getTotalCredits();
        String studentDesc = student.getStudentDesc();

        Object object[] = null;
        String sql = "";

        //totalCredits存在 studentDesc 不存在
        if ((totalCredits!=0 || totalCredits!=0.0) && (studentDesc==null || studentDesc=="")){
            object =  new Object[2];
            object[0] = totalCredits;
            object[1] = studentId;
            sql = "UPDATE studentinfo set totalCredits = ? where studentId = ?";
        }
        //studentDesc存在 totalCredits 不存在
        else if ((studentDesc!=null || studentDesc!="") && ((totalCredits==0 || totalCredits==0.0))){
            object =  new Object[2];
            object[0] = studentDesc;
            object[1] = studentId;
            sql = "UPDATE studentinfo set studentDesc = ? where studentId = ?";
        }
        //都存在
        else{
            object =  new Object[3];
            object[0] = totalCredits;
            object[1] = studentDesc;
            object[2] = studentId;
            sql = "UPDATE studentinfo set totalCredits = ? , studentDesc = ? where studentId = ?";
        }

        return  sqlUtil.execute(sql,object);
    }



    /*
    学生添加可选的课程
    需要参数 包含教师Id 学生Id  课程Id的类 selectCourse
    返回数据库修改成功个数
     */
    public int insertSelectCourse(SelectCourse selectCourse){
        Object object[] = new Object[]{selectCourse.getCourseId(),selectCourse.getStudentId(),selectCourse.getTeacherId(),1};
        String sql = "insert into selectCourseInfo(courseId,studentId,teacherId,status) values(?,?,?,?)";
        return sqlUtil.execute(sql,object);
    }


    /*
    学生查看id是否存在
    需要参数 教师Id
    返回数据库查询到的个数
     */
    public int queryByCourseId(SelectCourse selectCourse){
        Object object[] = new Object[]{selectCourse.getCourseId(),selectCourse.getStudentId()};
        String sql = "select count(courseId) FROM selectcourseinfo where courseId = ? and studentId = ?";
        ResultSet rs = sqlUtil.executeQuery(sql,object);

        //ResultSet解析出课程对象
        return SQLParse.parseCount(rs);
    }



    /*
    未登陆状态下 通过学生Id  学生姓名是否存在该人
    需要参数 学生Id 学生姓名
    返回 真假
     */
    public boolean selectStudentByIdAndName(String studentId, String studentName){

        Object object[] = new Object[]{studentId,studentName};

        String sql = "select *from studentInfo where studentId =? and StudentName = ?";

        ResultSet rs = sqlUtil.executeQuery(sql,object);

        try {
            if (rs.next()){
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }


    /*
    未登录状态下 修改登陆密码
    需要参数 学生Id 学生姓名 学生密码
    返回数据库修改成功个数
     */
    public int updatePasswordNoLogin(String studentId,String studentName, String studentPassword){
        Object object[] = new Object[]{studentPassword,studentId,studentName};
        String sql = "update studentInfo set studentPassword = ? where studentId =? and StudentName = ?";
        return sqlUtil.execute(sql,object);
    }




    /*
   辅助性公共方法 帮助解析ResultSet解析出Student对象
   需要参数 sql语句 对应的参数
   返回单个学生的信息
    */
    public Student selectStudent(Student student,String sql,Object[] object){


        ResultSet rs = sqlUtil.executeQuery(sql,object);

        Student stu = null;

        try {
            if (rs.next()) {
                String studentId =  rs.getString("studentId");
                String studentPassword = rs.getString("studentPassword");
                String studentName =  rs.getString("studentName");
                float totalCredits  = rs.getFloat("totalCredits");
                String studentMajor = rs.getString("studentMajor");
                String studentDesc  = rs.getString("studentDesc");

                stu = new Student();
                stu.setStudentId(studentId);
                stu.setStudentPassword(studentPassword);
                stu.setStudentName(studentName);
                stu.setTotalCredits(totalCredits);
                stu.setStudentMajor(studentMajor);
                stu.setStudentDesc(studentDesc);
            }
        }catch (SQLException e){
            e.printStackTrace();
        }

        return stu;
    }



}
